package com.mccraywebtech.email.impl;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.mccraywebtech.email.EmailService;
import com.mccraywebtech.models.Contact;
import com.mccraywebtech.models.EmailUser;
import com.mccraywebtech.services.impl.ContactServiceImpl;


@Service("emailService")
public class EmailServiceImpl implements EmailService<EmailUser> {
	@Autowired
	@Qualifier("contactDAO")
	private ContactServiceImpl contactService;
	private String authUser;
	private String authPwd;
	private String hostName;
	private String sender;
	private int smtpPort;
	private boolean ssl;
	private Email email;

	/**
	 * @return the contactService
	 */
	public ContactServiceImpl getContactService() {
		return contactService;
	}

	/**
	 * @param contactService the contactService to set
	 */
	public void setContactService(ContactServiceImpl contactService) {
		this.contactService = contactService;
	}
	

	/**
	 * @return the authUser
	 */
	public String getAuthUser() {
		return authUser;
	}
	
	/**
	 * @param authUser the authUser to set
	 */
	public void setAuthUser(String authUser) {
		this.authUser = authUser;
	}
	
	/**
	 * @return the authPwd
	 */
	public String getAuthPwd() {
		return authPwd;
	}
	
	/**
	 * @param authPwd the authPwd to set
	 */
	public void setAuthPwd(String authPwd) {
		this.authPwd = authPwd;
	}
	
	/**
	 * @return the hostName
	 */
	public String getHostName() {
		return hostName;
	}
	
	/**
	 * @param hostName the hostName to set
	 */
	public void setHostName(String hostName) {
		this.hostName = hostName;
	}
	
	/**
	 * @return the smtpPort
	 */
	public int getSmtpPort() {
		return smtpPort;
	}
	
	/**
	 * @param smtpPort the smtpPort to set
	 */
	public void setSmtpPort(int smtpPort) {
		this.smtpPort = smtpPort;
	}
	
	/**
	 * @return the ssl
	 */
	public boolean isSsl() {
		return ssl;
	}

	/**
	 * @param ssl the ssl to set
	 */
	public void setSsl(boolean ssl) {
		this.ssl = ssl;
	}

	/**
	 * @return the sender
	 */
	public String getSender() {
		return sender;
	}

	/**
	 * @param sender the sender to set
	 */
	public void setSender(String sender) {
		this.sender = sender;
	}

	/**
	 * @return the email
	 */
	public Email getEmail() {
		return email;
	}

	/**
	 * @param email
	 *            the email to set
	 */
	public void setEmail(Email email) {
		this.email = email;
	}

	@Override
	public void sendEmail(EmailUser euser) {
		// TODO Auto-generated method stub
		Contact c = contactService.getObject(euser.getUserId());
		try {
			email = new SimpleEmail();
			email.setAuthenticator(new DefaultAuthenticator(authUser, authPwd));
			email.setSmtpPort(smtpPort);
			email.setHostName(hostName);
			email.setFrom(sender);
			email.setMsg(euser.getMessage());
			email.setSubject(euser.getSubject());
			email.addTo(c.getEmail());
			email.send();
		} catch (EmailException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
